﻿using DevelopTool.Bll.DbBlls;
using DevelopTool.Common;
using DevelopTool.Forms.CommonForms;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.DbModels;
using DevelopTool.Model.Enums;
using Sunny.UI;
using System.Collections.Generic;
using System.Linq;

namespace DevelopTool.Forms.DbForms
{
    /// <summary>
    /// 资源分组帮助类
    /// </summary>
    public class DbSourceGroupHelper
    {
        /// <summary>
        /// 添加资源到分组
        /// </summary>
        /// <param name="dataBase"></param>
        /// <param name="sourcName"></param>
        /// <param name="sourceType"></param>
        /// <param name="dbInfo"></param>
        public static void AddSourceToGroup(string dataBase,string sourcName,string sourceType,DbInfo dbInfo,string tableName = null)
        {
            DbSourceGroupBll dbSourceGroupBll = new DbSourceGroupBll();
            List<DbSourceGroup> dbSourceGroups = dbSourceGroupBll.GetList(MenuNodeType.Folder).Where(it => it.DbInfo.Id == dbInfo.Id).ToList();

            ChoiceMenuForm choiceMenuForm = new ChoiceMenuForm("添加到分组",sourcName,dbSourceGroups.Cast<ManageMenu>(),false,false);
            choiceMenuForm.FolderText = "分组";
            choiceMenuForm.TopTreeNodeText = "资源分组列表";
            choiceMenuForm.NavMenuExpandAll = true;
            choiceMenuForm.MenuAdd += delegate (object ss,MenuEventArgs ee)
            {
                DbSourceGroup dbSourceGroup = null;

                if(ee.ManageMenu.NodeType == MenuNodeType.Folder)//新建目录
                {
                    dbSourceGroup = new DbSourceGroup()
                    {
                        NodeName = ee.ManageMenu.NodeName,
                        NodeType = MenuNodeType.Folder,
                        NodePid = ee.ManageMenu.NodePid,
                        SourceType = "TGroupFolder",
                        DbInfo = dbInfo,
                        TableName = tableName
                    };
                }
                else//添加节点
                {
                    DbSourceGroup topDbSourceGroup = dbSourceGroupBll.GetById(ee.ManageMenu.NodePid);
                    if(topDbSourceGroup.NodePid == 0)
                    {
                        UINotifierHelper.ShowNotifier($"不能添加资源到数据库连接，请选择子级分组",UINotifierType.WARNING,UIStyles.CurrentResources.InfoTitle,false,2000);
                        ee.Cancel = true;
                        return;
                    }

                    if(dbSourceGroupBll.GetList(MenuNodeType.Node).Exists(it => it.DbInfo.Id == dbInfo.Id && it.DataBaseName == dataBase && it.NodeName == sourcName && it.SourceType == sourceType && it.NodePid == ee.ManageMenu.NodePid && it.TableName == tableName))
                    {
                        UINotifierHelper.ShowNotifier($"该资源已存在，请勿重复添加",UINotifierType.WARNING,UIStyles.CurrentResources.InfoTitle,false,2000);
                        ee.Cancel = true;
                        return;
                    }

                    dbSourceGroup = new DbSourceGroup()
                    {
                        NodeName = sourcName,
                        NodeType = MenuNodeType.Node,
                        NodePid = ee.ManageMenu.NodePid,
                        DataBaseName = dataBase,
                        SourceType = sourceType,
                        DbInfo = dbInfo,
                        TableName = tableName
                    };
                }

                ee.ManageMenu = dbSourceGroupBll.Save(dbSourceGroup);
                UINotifierHelper.ShowNotifier($"【{dbSourceGroup.NodeName}】添加成功",UINotifierType.INFO,UIStyles.CurrentResources.InfoTitle,false,2000);
            };

            choiceMenuForm.ShowDialog();
        }

    }
}
